/**
 * 人员添加身份证明图片
 * Created by lishun on 2017/9/11 0011.
 */
var idName = "#uploadBtn";
var descData;
var showDom;
var ctx;
// 按钮设置
function setImgBtn(itemDom,ctxUrl) {
    if(ctxUrl != null){
        ctx = ctxUrl;
    }
    if($("#uploadImg2").attr("status") == 1 && $("#uploadImg1").attr("status") == 1){
        $(itemDom).hide();
    } else {
        $(itemDom).show();
    }
    if($("#uploadImg1").attr("status") == 0){
        $(itemDom).val("上传正面图");
        showDom = "#uploadImg1";
        descData = "{desc:'证件类型：身份证正面照'}";
        return;
    };
    if($("#uploadImg2").attr("status") == 0){
        $(itemDom).val("上传反面图");
        showDom = "#uploadImg2";
        descData = "{desc:'证件类型：身份证反面照'}";
        return;
    }
}
//上传
layui.use('upload', function(){
    var $ = layui.jquery,upload = layui.upload;
    var uploadInst = upload.render({
        elem: idName
        ,accept:'images'
        ,data:descData
        ,method: 'post'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $(showDom).attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(data){
            //上传成功
            if(data.status == 'SUCCESS'){
                $(showDom).attr("status",'1');// 上传状态修改为已上传图片
                // $(showDom).attr("src",ctx + "/common/getMGFile/"+ data.data +"/" + $(showDom).width() + "x" + $(showDom).height() +".do");
                $(showDom).attr("src",ctx + "/common/getMGFile/"+ data.data +"/100x100.do");
                $(showDom).attr("layer-src",ctx + "/common/getMGFile/"+ data.data +"/600x600.do");
                $(showDom).parent().children(".remove").show(); //显示修改按钮
                $(showDom).parent().children(".mongoCode").val(data.data); //添加图片信息
                setImgBtn(idName);
                layer.photos({
                    photos: '#layer-photos-demo',
                    shift:5
                });
                return;
            }
            //如果上传失败
            removeInfo($(showDom).parent().children("button"));
        }
        ,error: function(){
            removeInfo($(showDom).parent().children("button"));
        }
    });
});

// 失败 移除信息
function removeInfo(removeDom) {
    $(removeDom).parent().children("img").attr("status",0); // 上传状态修改为已上传图片
    //默认显示图片地址
    $(removeDom).parent().children("img").removeAttr("src");
    $(removeDom).parent().children("img").removeAttr("layer-src");
    $(removeDom).hide();
    $(removeDom).parent().children(".mongoCode").removeAttr("value");
    setImgBtn(idName);
    return layer.msg('上传失败');
}
